package xiangmu.schoolpaper.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xiangmu.schoolpaper.exception.ContentNullException;
import xiangmu.schoolpaper.mapper.StudentDataListMapper;
import xiangmu.schoolpaper.pojo.DTO.StudentDetailScoreDTO;
import xiangmu.schoolpaper.pojo.VO.StudentDetailScoreVO;
import xiangmu.schoolpaper.pojo.VO.StudentOneScoreVO;
import xiangmu.schoolpaper.service.StudentDataListService;

import java.util.List;

@Service
public class StudentDataListServiceImpl implements StudentDataListService {
    @Autowired
    private StudentDataListMapper studentDataListMapper;

    @Override
    public String download(Integer paperId) {
        if (paperId == null) {
            throw new ContentNullException("论文ID（paperId）不能为空，请传入有效的论文ID");
        }
        return studentDataListMapper.download(paperId);
    }

    @Override
    public List<StudentOneScoreVO> getOneScore(Integer studentId) {
        if (studentId == null) {
            throw new ContentNullException("学生ID（studentId）不能为空，请传入有效的学生ID");
        }
        return studentDataListMapper.getOneScore(studentId);
    }

    @Override
    public List<StudentDetailScoreVO> getDetailScore(Integer studentId, Integer paperId) {
        // 1. 新增：检查studentId是否为空，为空则抛异常
        if (studentId == null) {
            throw new ContentNullException("学生ID（studentId）不能为空，请传入有效的学生ID");
        }
        // 2. 新增：检查paperId是否为空，为空则抛异常
        if (paperId == null) {
            throw new ContentNullException("论文ID（paperId）不能为空，请传入有效的论文ID");
        }

        // 直接传递两个参数给Mapper查询
        List<StudentDetailScoreVO> scoreList = studentDataListMapper.getDetailScore(studentId, paperId);

        // 业务逻辑校验：确保数据不为空
        if (scoreList.isEmpty()) {
            throw new ContentNullException("该学生的此论文暂无二级指标分数数据，请检查论文是否已上传并评分");
        }

        // 需求补充：100分不返回建议（仅保留分数和依据）
        scoreList.forEach(vo -> {
            if (vo.getScore() != null && vo.getScore() == 100) {
                vo.setSuggest(null); // 100分时置空建议
            }
        });

        return scoreList;
    }
}
